<template>
	<div class="openClass">
		<div class="u_resourceH classH clear">
			<div class="u_resourceH_l fl">
				<template v-if="!$store.state.isSchool">
					<el-select filterable placeholder="请选择" v-model="subjectId" @change="changeSelect">
						<el-option v-for="item in subjectList" :key="item.id" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
				</template>
			</div>
			<!--  -->
			<div class="u_resourceH_r fr">
				<el-input size="medium" placeholder="教学资源名称" suffix-icon="el-icon-search" v-model="resourceName" @change="changInputName">
				</el-input>
			</div>
		</div>
		<!--  -->
		<div class="classBox" v-loading="loading">
			<ul>
				<li class="classLs clear" v-for="(item, index) in favouriteList" :key="item.id">
					<span class="classLs_img">
						<img v-if="item.file_type < 3" src="../../../public/images/format/resourceicon1.png">
						<img v-else-if="item.file_type === 3" src="../../../public/images/format/resourceicon3.png">
						<img v-else-if="item.file_type === 6" src="../../../public/images/format/resourceicon6.png">
					</span>
					<div class="classLs_info resource_g">
						<div class="classLs_1 clear">
							<a target="_blank" @click="pathURL(item.id)">{{ item.res_name }}</a>
							<div class="resource_ioperation fr">
								<el-popover placement="bottom" width="" popper-class="ioperation_hover" trigger="hover">
									<ul class="r_ioperation">
										<li @click="cancelFav(item.id)">取消收藏</li>
									</ul>
									<i slot="reference" class="el-icon-arrow-down"></i>
								</el-popover>
							</div>
						</div>
						<div class="classLs_4">
							<span v-if="item.sub_name">{{ item.sub_name }}</span>
							<span>分享：{{ item.create_name }}</span>
							<span>大小 {{ ByteSwitch(item.filesize) }}</span>
							<span>收藏 {{ item.favs }}</span>
						</div>
						<div class="classLs_3" v-if="item.profile != ''">
							<el-tooltip class="item" effect="dark" :content="item.profile" placement="top-start">
								<p>简介：{{item.profile}}</p>
							</el-tooltip>
						</div>
					</div>
				</li>
			</ul>
		</div>
		<!--  -->
		<div class="pageing" v-if="total_number > 10">
			<el-pagination @current-change="change_page" :current-page="page_index" :page-size="page_size" layout="prev, pager, next" :total="total_number">
			</el-pagination>
		</div>
		<div class="dependent" v-if="placeholder">
			<i class="ipm image_nofound"></i>
			<p>
				还没发现任何相关的内容哦~
			</p>
		</div>
	</div>
</template>
<script>
export default {
	name: '',
	data() {
		return {
			origin: 1,
			subjectId: 0, //学科
			subjectList: this.$g.subjectList, //学科表
			resourceName: '', //资源名称
			page_index: 1,
			page_size: this.$g.page_size,
			favouriteList: [],
			total_number: 0,
			placeholder: false,
			loading: true
		}
	},
	created() {

		this.getFavourite()
	},
	methods: {
		pathURL(id) {
			let host = sessionStorage.getItem('host')
			let redirectParams = sessionStorage.getItem('redirect_params')
			let link = `${this.BASE_URL}${redirectParams}&host=${host}&routerUrl=/home/resourceDetail&id=${id}`
			window.open(link)
		},

		getFavourite() {
			let success = (res => {
				var data = res.data;
				this.favouriteList = data.data;
				this.total_number = data.data_sum
				this.page_index = parseInt(data.page_index);
				this.loading = false
				this.placeholder = this.favouriteList.length === 0 ? true : false
				this.subjectList = this.$g.subjectList
			})
			this.$ajax({
				url: '/api/favourite/favourite',
				params: {
					user_id: this.$g.user_id,
					origin: this.origin,
					page: this.page_index,
					size: this.page_size,
					keyword: this.resourceName,
					subject_id: this.subjectId,
					data_type: 2
				},
				func: { success: success }
			})
		},
		change_page(val) {
			this.page_index = val
			this.getFavourite();
		},
		changInputName() {
			this.page_index = 1
			this.getFavourite();
		},
		changeSelect(val) {
			this.page_index = 1
			this.getFavourite();
		},
		cancelFav(res_id) {
			this.$confirm('此操作是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let success = (res => {
					this.$message.success(res.msg)
					this.page_index = 1
					this.getFavourite();
				})
				this.$ajax({
					url: '/api/favourite/favourite',
					method: 'delete',
					params: {
						user_id: this.$g.user_id,
						origin: this.origin,
						res_id: res_id
					},
					func: {
						success: success
					}
				})
			}).catch(() => {});
		},
		//MB单位转换
		ByteSwitch(bytes, decimals) {
			bytes = parseInt(bytes);
			decimals = parseInt(decimals);
			if (decimals > 8) {
				decimals = 8;
			}
			if (isNaN(decimals)) {
				decimals = 2;
			}
			var sz = ['B', 'K', 'M', 'G', 'T', 'P'];
			var factor = Math.floor(((bytes + '').length - 1) / 3);
			var result = bytes / Math.pow(1024, factor) + '';
			var index = result.indexOf('.');
			if (index == -1) {
				index = result.length - decimals - 1;
			}
			result = result.substr(0, index + decimals + 1);
			return result + sz[factor];

			var bytes = parseInt(val);

			if (bytes === 0) { return '0 B' };
			var k = 1024;
			var sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
			var i = Math.floor(Math.log(bytes) / Math.log(k))
			//return (bytes / Math.pow(k, i)) + ' ' + sizes[i];
			var sum = (bytes / Math.pow(k, i)).toPrecision(3)
			// console.info(sum)
			return sum + ' ' + sizes[i];
			//toPrecision(3) 后面保留两位小数，如1.00GB
		},
	}
}
</script>